package com.sky.config;

import com.sky.properties.AliOssProperties;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类，用于创建 AliOssUtil 对象
 */
@Configuration
@Slf4j
public class OssConfiguration {

//    @Bean
//    @ConditionalOnBean
//    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {
//        log.info("开始创建阿里云文件上传工具类对象：{}", aliOssProperties);
//        return new AliOssUtil(aliOssProperties.getEndpoint(),
//                aliOssProperties.getAccessKeyId(),
//                aliOssProperties.getAccessKeySecret(),
//                aliOssProperties.getBucketName()
//        );
//    }

    @Bean
    @ConditionalOnMissingBean
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties) {
        if (aliOssProperties == null) {
            log.error("AliOssProperties 未正确注入，无法创建 AliOssUtil");
            throw new IllegalArgumentException("AliOssProperties 不能为 null");
        }

        String endpoint = aliOssProperties.getEndpoint();
        String accessKeyId = aliOssProperties.getAccessKeyId();
        String accessKeySecret = aliOssProperties.getAccessKeySecret();
        String bucketName = aliOssProperties.getBucketName();

        if (endpoint == null || accessKeyId == null || accessKeySecret == null || bucketName == null) {
            log.error("AliOssProperties 中存在空字段，无法创建 AliOssUtil");
            throw new IllegalArgumentException("AliOssProperties 中的字段不能为空");
        }

        log.info("开始创建阿里云文件上传工具类对象，Bucket: {}", bucketName);
        return new AliOssUtil(endpoint, accessKeyId, accessKeySecret, bucketName);
    }

}